package com.sjme.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sjme.domain.user.User;
import com.sjme.domain.user.dto.UserDto;
import com.sjme.domain.user.vo.UserVo;
import com.sjme.service.IUserService;
import com.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static com.constant.ResultConstant.ERROR;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author LY
 * @since 2025-10-10
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService userService;
    
    @PostMapping("/queryUser")
    public Result<IPage<User>> query(@RequestBody UserDto User) {
        IPage<User> result = userService.queryUser(User);
        return Result.success(result);
    }

    @PostMapping("/addUser")
    public Result add(@RequestBody User User) {
        try {
            userService.save(User);
        } catch (Exception e) {
            log.error("addUser Exception", e);
            return Result.error(ERROR, e.getMessage());
        }
        return Result.success("新增成功！");
    }

    @PostMapping("/editUser")
    public Result edit(@RequestBody User User) {
        try {
            userService.updateById(User);
        } catch (Exception e) {
            log.error("editUser Exception", e);
            return Result.error(ERROR, e.getMessage());
        }
        return Result.success("修改成功！");
    }

    @PostMapping("/deleteUser")
    public Result delete(@RequestBody List<String> ids) {
        try {
            userService.removeByIds(ids);
        } catch (Exception e) {
            log.error("deleteUser Exception", e);
            return Result.error(ERROR, e.getMessage());
        }
        return Result.success("修改成功！");
    }

    @PostMapping("/queryUserByCondition")
    public List<User> queryUserByCondition(@RequestBody User User) {
        List<User> userList = null;
        try {
            userList = userService.queryUserByCondition(User);
        } catch (Exception e) {
            log.error("queryUserByCondition Exception", e);
        }
        return userList;
    }

    @PostMapping("/queryUserByIds")
    public List<User> queryUserByIds(@RequestBody List<String> userIdList) {
        return userService.listByIds(userIdList);
    }

    @PostMapping("/login")
    public Result<UserVo> login(@RequestBody User user) {
        try {
            UserVo userInfo = userService.login(user);
            return Result.success(userInfo);
        } catch (Exception e) {
            log.error("login Exception", e);
            return Result.error(ERROR, e.getMessage());
        }
    }
}
